const str = {
  isJson(s: any) {
    try {
      JSON.parse(s);
      return true;
    } catch (err) {
      return false;
    }
  },
  /**
   * 是否是自然数,0,1,2...
   * @param {*} s
   * @returns {boolean}
   */
  isNaturalNumber(s: any) {
    return /^[1-9]\d*|0$/.test("" + s);
  },

  isUrl(s: any) {
    return /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(s);
  },

  now() {
    return Date.now();
  },
  /**
   * 用 str 填充到 objStr，直到 长度为 len
   * @param {*} objStr
   * @param {*} str
   * @param {*} len
   * @param {string} d l 左边；r 右边
   */
  padding(objStr: any, str: any, len: any, d = "l") {
    if (objStr.length >= len) {
      return objStr;
    }
    while (objStr.length < len) {
      if (d == "l") {
        objStr = str + objStr;
      } else {
        objStr += str;
      }
    }
    return objStr;
  },

  randInt: (min: any, max: any) => {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },

  uuid: () => {
    return "" + str.now() + str.randInt(100000, 999999);
  },
};
export default str;
